package com.zonhar.autofeed.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zonhar.autofeed.dao.SensorDao;
import com.zonhar.autofeed.entities.entity.SensorEntity;
import com.zonhar.autofeed.service.SensorService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 传感器服务
 * @author Chenhua
 * @date 2023-04-
 */
@Service
public class SensorServiceImpl extends ServiceImpl<SensorDao, SensorEntity> implements SensorService {

    @Override
    public List<SensorEntity> sensorList() {
        QueryWrapper<SensorEntity> sensorEntityQueryWrapper =new QueryWrapper<>();
        sensorEntityQueryWrapper.orderByAsc("sort");
        List<SensorEntity> list = list(sensorEntityQueryWrapper);
        return list.stream().peek(e -> {
            Integer stat = e.getStat();
            e.setButton1Name(stat.equals(0) ? "停用" : "启用");
        }).collect(Collectors.toList());
    }

    @Override
    public SensorEntity updateStat(Long id) {
        SensorEntity sensorEntity = getById(id);
        sensorEntity.setStat(sensorEntity.getStat().equals(0) ? 1 : 0);
        sensorEntity.setButton1Name(sensorEntity.getStat().equals(0) ? "停用" : "启用");
        updateById(sensorEntity);
        return sensorEntity;
    }

}
